package org.stvd.service.common.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.regex.Pattern;

import javax.annotation.Resource;

import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.stvd.common.utils.ServiceResult;
import org.stvd.core.dto.QueryResult;
import org.stvd.core.util.StringUtil;
import org.stvd.entities.common.CodeTable;
import org.stvd.repository.common.CodeTableDao;
import org.stvd.service.common.CodeTableService;

@Service("codeTableService")
public class CodeTableServiceImpl implements CodeTableService {

    @Resource(name = "CodeTableDao")
    private CodeTableDao codeTableDao;

    @Override
    public CodeTable findById(String tableName, String id) throws Exception {
        return codeTableDao.findById(tableName, id);
    }

    @Override
    public QueryResult<CodeTable> getQueryResult(String tableName, int start, int limit, String id, String name,
            String pym) throws Exception {
        return codeTableDao.getQueryResult(tableName, start, limit, id, name, pym);
    }

    @Override
    @Transactional
    @CacheEvict(value = "adminCache", key = "'CodeTableService.findAll('+#tableName+')'")
    public int insert(String tableName, String id, String name, String pym, int pxh) throws Exception {
        return codeTableDao.insert(tableName, id, name, pym, pxh);
    }

    @Override
    @Transactional
    @CacheEvict(value = "adminCache", key = "'CodeTableService.findAll('+#tableName+')'")
    public int update(String tableName, String id, String name, String pym, int pxh) throws Exception {
        return codeTableDao.update(tableName, id, name, pym, pxh);
    }

    @Override
    @Transactional
    @CacheEvict(value = "adminCache", key = "'CodeTableService.findAll('+#tableName+')'")
    public int delete(String tableName, String id) throws Exception {
        return codeTableDao.delete(tableName, id);
    }

    @Override
    @Cacheable(value = "adminCache", key = "'CodeTableService.findAll('+#tableName+')'")
    public List<CodeTable> findAll(String tableName) throws Exception {
        return codeTableDao.findAll(tableName, "", "", "");
    }

    @Override
    public List<CodeTable> findAll(String tableName, String id, String name, String pym) throws Exception {
        return codeTableDao.findAll(tableName, id, name, pym);
    }

    @Override
    public List<CodeTable> getCodeTableListByRegExp(List<CodeTable> listAll, String regex) {
        List<CodeTable> listResult = new ArrayList<CodeTable>();
        Pattern pattern = Pattern.compile(regex);
        for (CodeTable codeTable : listAll) {
            if (pattern.matcher(codeTable.getId()).matches()) {
                listResult.add(codeTable);
            }
        }
        return listResult;
    }

    @Override
    public List<Map<String, Object>> listAllCodeTable(String tableName) {
        return codeTableDao.listAllCodeTable(tableName);
    }

    @Override
    public Map<String, Object> getCodeTableByName(String tableName) {
        return codeTableDao.getCodeTableByName(tableName);
    }
    
    @Override
    public ServiceResult<Map<String, Object>> insertCodeTableType(CodeTable codeTable) {
        ServiceResult<Map<String, Object>> serviceResult = new ServiceResult<Map<String, Object>>();
        if(codeTable==null || StringUtil.isEmpty(codeTable.getId()) || StringUtil.isEmpty(codeTable.getName())) {
            serviceResult.setParamError();
            return serviceResult;
        }
        if(codeTableDao.getCodeTableByName(codeTable.getId())!=null) {
            serviceResult.setDataError("当前码表已存在");
            return serviceResult;
        }
        codeTableDao.insertCodeTable(codeTable.getId(), codeTable.getName());
        serviceResult.setMessage("新增成功");
        return serviceResult;
    }

    @Override
    public ServiceResult<Map<String, Object>> updateCodeTableType(CodeTable codeTable) {
        ServiceResult<Map<String, Object>> serviceResult = new ServiceResult<Map<String, Object>>();
        if(codeTable==null || StringUtil.isEmpty(codeTable.getId()) || StringUtil.isEmpty(codeTable.getName())) {
            serviceResult.setParamError();
            return serviceResult;
        }
        codeTableDao.updateCodeTable(codeTable.getId(), codeTable.getName());
        serviceResult.setMessage("修改成功");
        return serviceResult;
    }

    @Override
    public ServiceResult<Map<String, Object>> deleteCodeTableType(String id) {
        ServiceResult<Map<String, Object>> serviceResult = new ServiceResult<Map<String, Object>>();
        if(StringUtil.isEmpty(id)) {
            serviceResult.setParamError();
            return serviceResult;
        }
        codeTableDao.deleteCodeTable(id);
        serviceResult.setMessage("删除成功");
        return serviceResult;
    }

}
